package com.blog.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.dao.MLyMapper;
import com.blog.pojo.MLy;
import com.blog.service.ICommentService;

@Service
public class CommentService implements ICommentService{
	
	@Autowired
	private MLyMapper lyMapper;
	
	@Override
	public List<MLy> insertAndSelect(String comments, String name) {
		
		MLy ly = new MLy();
		ly.setContent(comments);
		ly.setCreated(new Date());
		ly.setDisplay(1);
		ly.setIp("127.0.0.1");
		ly.setName(name);
		ly.setPid(0);
		ly.setZan(0);
		
		lyMapper.insert(ly);
		
		List<MLy> lyList = selectAll();
		return lyList;
	}

	
	@Override
	public List<MLy> selectAll() {
		
		List<MLy> lyLists = lyMapper.selectOrderByTime();
		return lyLists;
	}


	@Override
	public Map praise(Integer id,Integer type) {
		
		Map map = new HashMap();	
		
		int zan = lyMapper.selectZanById(id);
		int res = 0;
		
		if(type == 0){
			//0为增加
			res = lyMapper.updatePraiseById(id,zan);
		}else{
			//1 为减少
			res = lyMapper.updatePraiseDeById(id,zan);
		}
		
		if(res == 1){
			map.put("status", 0);
		}else{
			map.put("status", 1);
		}
		return map;
	}

}
